// Package api internal/api/operation_log_api.go
package api

import (
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"gozrd-admin-api/internal/entity/dto"
	"gozrd-admin-api/internal/repository"
	"gozrd-admin-api/internal/service"
	"gozrd-admin-api/pkg/response"
	"strconv"
)

// OperationLogAPI 操作日志控制器
type OperationLogAPI struct {
	operationLogService service.ISysOperationLogService
}

// NewOperationLogAPI 创建操作日志控制器
func NewOperationLogAPI(db *gorm.DB) *OperationLogAPI {
	operationLogRepo := repository.NewOperationLogRepository(db)
	operationLogService := service.NewOperationLogService(operationLogRepo)
	return &OperationLogAPI{
		operationLogService: operationLogService,
	}
}

// GetSysOperationLogList 分页获取操作日志列表
// @Summary 分页获取操作日志列表接口
// @Description 分页获取操作日志列表数据
// @Tags 操作日志
// @Accept json
// @Produce json
// @Param pageNum query int false "页码"
// @Param pageSize query int false "每页数量"
// @Param username query string false "用户名"
// @Param beginTime query string false "开始时间"
// @Param endTime query string false "结束时间"
// @Success 200 {object} response.Response "返回操作日志列表"
// @Router /operationLog/list [get]
func (l *OperationLogAPI) GetSysOperationLogList(c *gin.Context) {
	// 解析分页参数
	pageNum, _ := strconv.Atoi(c.DefaultQuery("pageNum", "1"))
	pageSize, _ := strconv.Atoi(c.DefaultQuery("pageSize", "10"))

	// 解析查询条件
	username := c.Query("username")
	beginTime := c.Query("beginTime")
	endTime := c.Query("endTime")

	l.operationLogService.GetSysOperationLogList(c, username, beginTime, endTime, pageSize, pageNum)
}

// DeleteSysOperationLogById 根据ID删除操作日志
// @Summary 根据ID删除操作日志接口
// @Description 删除指定ID的操作日志
// @Tags 操作日志
// @Accept json
// @Produce json
// @Param data body dto.SysOperationLogIdDto true "操作日志ID"
// @Success 200 {object} response.Response "删除成功"
// @Router /operationLog [delete]
func (l *OperationLogAPI) DeleteSysOperationLogById(c *gin.Context) {
	var idDTO dto.SysOperationLogIdDto
	if err := c.ShouldBindJSON(&idDTO); err != nil {
		response.Fail(c, response.ErrParams, err.Error())
		return
	}

	l.operationLogService.DeleteSysOperationLogById(c, idDTO)
}

// BatchDeleteSysOperationLog 批量删除操作日志
// @Summary 批量删除操作日志接口
// @Description 批量删除操作日志
// @Tags 操作日志
// @Accept json
// @Produce json
// @Param data body dto.DelSysOperationLogDto true "ID列表"
// @Success 200 {object} response.Response "批量删除成功"
// @Router /operationLog/batch [delete]
func (l *OperationLogAPI) BatchDeleteSysOperationLog(c *gin.Context) {
	var deleteDTO dto.DelSysOperationLogDto
	if err := c.ShouldBindJSON(&deleteDTO); err != nil {
		response.Fail(c, response.ErrParams, err.Error())
		return
	}

	l.operationLogService.BatchDeleteSysOperationLog(c, deleteDTO)
}

// CleanSysOperationLog 清空操作日志
// @Summary 清空操作日志接口
// @Description 清空所有操作日志
// @Tags 操作日志
// @Accept json
// @Produce json
// @Success 200 {object} response.Response "清空成功"
// @Router /operationLog/clean [delete]
func (l *OperationLogAPI) CleanSysOperationLog(c *gin.Context) {
	l.operationLogService.CleanSysOperationLog(c)
}

// SetOperationLogServiceForTest 设置操作日志服务，仅供测试使用
func SetOperationLogServiceForTest(l *OperationLogAPI, svc service.ISysOperationLogService) {
	l.operationLogService = svc
}
